package com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.service;

import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdeks_gettoken;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdesk_delete_gettoken;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdesk_screen_VideoSteam;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdesk_sreen_delete_videoStream;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.mapper.Frontdesk_cameraMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Service
public class Frontdesk_sreenVideoSream_deleteService {

    @Autowired
    Frontdesk_cameraMapper frontdeskCameraMapper;
    public String gettoken(String pid,Long currentTimeSeconds,String vid){

        Frontdesk_delete_gettoken frontdeskDeleteGettoken = new Frontdesk_delete_gettoken();
        frontdeskDeleteGettoken.setChannel("1551505304@qq.com");
        frontdeskDeleteGettoken.setPId(pid);
        frontdeskDeleteGettoken.setCId(1);
        frontdeskDeleteGettoken.setSecret("89e094fe21278503d8d0010af9545231");
        frontdeskDeleteGettoken.setT(currentTimeSeconds);
//        frontdeskDeleteGettoken.setT(currentTimeSeconds);
        frontdeskDeleteGettoken.setVId(vid);
// 拼接字符串
        String strToHash = "cId=" + frontdeskDeleteGettoken.getCId()
                + "&channel=" + frontdeskDeleteGettoken.getChannel()
                + "&pId=" + frontdeskDeleteGettoken.getPId()
                + "&secret=" + frontdeskDeleteGettoken.getSecret()
                + "&t=" + frontdeskDeleteGettoken.getT()
                + "&vId=" + frontdeskDeleteGettoken.getVId();

//        System.out.println(strToHash);
//        System.out.println(md5Hex(strToHash));
        // 返回加密后的token
        return md5Hex(strToHash);
    }

    public Frontdesk_sreen_delete_videoStream getVideoSteam(String station_id){

        String vId_token = frontdeskCameraMapper.selectTokenByStationId(station_id);
        String pid = frontdeskCameraMapper.selectCameraIdByStationId(station_id);

        long currentTimeMillis = System.currentTimeMillis();
        // 将毫秒级时间戳转换为秒级时间戳
        long currentTimeSeconds = currentTimeMillis / 1000;
        Frontdesk_sreen_delete_videoStream frontdeskSreenDeleteVideoStream = new Frontdesk_sreen_delete_videoStream();
        frontdeskSreenDeleteVideoStream.setChannel("1551505304@qq.com");
        frontdeskSreenDeleteVideoStream.setPId(pid);
        frontdeskSreenDeleteVideoStream.setCId(1);
        frontdeskSreenDeleteVideoStream.setToken(this.gettoken(pid,currentTimeSeconds,vId_token));
        frontdeskSreenDeleteVideoStream.setT(currentTimeSeconds);
        frontdeskSreenDeleteVideoStream.setVId(vId_token);
//        System.out.println(frontdeskSreenDeleteVideoStream);

        return frontdeskSreenDeleteVideoStream;
    }









    //md5方法：
    private String md5Hex(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(input.getBytes());
            return byteArrayToHex(hashBytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String byteArrayToHex(byte[] byteArray) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : byteArray) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}
